﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;

namespace WeChat.Common
{
    /// <summary>
    /// 通过活动及页面级别
    /// </summary>
    public class ApplicationLog
    {
        /// <summary>
        /// 写入错误日志
        /// </summary>
        /// <param name="message">日志内容</param>
        public static void Write(string message)
        {
            Write(message, LogType.txt);
        }

        /// <summary>
        /// 写入错误日志
        /// </summary>
        /// <param name="message">日志内容</param>
        /// <param name="method">方法名称</param>
        public static void Write(string message, string method)
        {
            Write(message + ",Method:" + method, LogType.txt);
        }

        /// <summary>
        /// 写入错误日志
        /// </summary>
        /// <param name="description">错误异常的简单描述</param>
        /// <param name="exceptionInfo">exception抛出的异常信息</param>
        /// <param name="pageName">报错页面的名称</param>
        /// <param name="methodName">报错方法的名称</param>
        public static void Write(string description, string exceptionInfo, string pageName, string methodName)
        {
            Write(string.Format("{0}:{1},Page={2},Method={3}", description, exceptionInfo, pageName, methodName));
        }

        /// <summary>
        /// 写入信息记录日志
        /// </summary>
        /// <param name="message">日志内容</param>
        public static void WriteLog(string message)
        {
            Write(message, LogType.log);
        }

        /// <summary>
        /// 写入信息记录日志
        /// </summary>
        /// <param name="message">日志内容</param>
        /// <param name="method">方法名称</param>
        public static void WriteLog(string message, string method)
        {
            Write(message + ",Method:" + method, LogType.log);
        }

        // 写入日志
        private static void Write(string message, LogType fileType)
        {
            string folder = "";
            try
            {
                folder = HttpContext.Current.Server.MapPath("~/Logs/");
            }
#pragma warning disable CS0168 // 声明了变量“ex”，但从未使用过
            catch (Exception ex)
#pragma warning restore CS0168 // 声明了变量“ex”，但从未使用过
            {
                folder = Path.Combine(HttpRuntime.AppDomainAppPath,"Logs\\");
            }
            string file = string.Format("{0}{1}.{2}", folder, DateTime.Now.ToString("yyyyMMdd"), fileType);
            if (HttpContext.Current != null)
            {
                message = string.Format("[{0}][{1}][{2}]:{3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), HttpContext.Current.Request.Url.LocalPath, HttpContext.Current.Request.UserHostAddress, message);
            }
            else
            {
                message = string.Format("[{0}]:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), message);
            }
            try
            {
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }

                StreamWriter sw;

                if (!File.Exists(file))
                {
                    sw = File.CreateText(file);
                    sw.Close();
                }

                sw = new StreamWriter(file, true, Encoding.Default);
                lock (sw)
                {
                    sw.WriteLine(message);
                    sw.Close();
                }
            }
#pragma warning disable CS0168 // 声明了变量“ex”，但从未使用过
            catch (Exception ex)
#pragma warning restore CS0168 // 声明了变量“ex”，但从未使用过
            {
                //if (HttpContext.Current != null)
                //{
                //    HttpContext.Current.Response.Write(string.Format("log write error, ex={0}。", ex.Message));
                //    HttpContext.Current.Response.End();
                //}
            }
        }

        //日志类型
        private enum LogType
        {
            txt = 0,
            log = 1
        }

    }

}
